home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / security / netlog-1.02 / lib / getif.c next >
Encoding:
C/C++ Source or Header  |  1993-05-20  |  755 b   |  45 lines

  1. #include <sys/types.h>
  2. #include <sys/socket.h>
  3. #include <net/if.h>
  4. #include <sys/sockio.h>
  5. #include <errno.h>
  6. #include <string.h>
  7.  
  8. char *
  9. getdefaultif(char *sbuf)
  10. {
  11.      char *interface = "le0";
  12.      struct ifconf ifc;
  13.      char buf[128];
  14.      int sock;
  15.      
  16.      if((sock = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
  17.       perror("socket");
  18.      else {
  19.       ifc.ifc_len = sizeof(buf);
  20.       ifc.ifc_buf = buf;
  21.  
  22.       if(ioctl(sock, SIOCGIFCONF, (char *)&ifc) == -1)
  23.            perror("ioctl");
  24.       else
  25.            interface = ifc.ifc_req->ifr_name;
  26.       close(sock);
  27.      }
  28.      if(sbuf){
  29.       strcpy(sbuf, interface);
  30.       return sbuf;
  31.      }
  32.      else {
  33.       return strdup(interface);
  34.      }
  35. }
  36.  
  37. #ifdef TEST_VERSION
  38.  
  39. main()
  40. {
  41.      printf("%s\n", getdefaultif((char *)0));
  42. }
  43.  
  44. #endif
  45.